package com.ikun.aimian.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ikun.aimian.annotation.AuthCheck;
import com.ikun.aimian.common.BaseResponse;
import com.ikun.aimian.common.DeleteRequest;
import com.ikun.aimian.common.ErrorCode;
import com.ikun.aimian.common.ResultUtils;
import com.ikun.aimian.exception.BusinessException;
import com.ikun.aimian.exception.ThrowUtils;
import com.ikun.aimian.model.dto.feedback.FeedbackAddRequest;
import com.ikun.aimian.model.dto.feedback.FeedbackQueryRequest;
import com.ikun.aimian.model.entity.Feedback;
import com.ikun.aimian.model.vo.FeedbackVO;
import com.ikun.aimian.service.FeedbackService;
import com.ikun.aimian.constant.UserConstant;
import com.ikun.aimian.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/feedback")
@Slf4j
public class FeedbackController {

    @Resource
    private FeedbackService feedbackService;

    @Resource
    private UserService userService;

    /**
     * 用户提交反馈
     */
    @PostMapping("/add")
    public BaseResponse<Long> addFeedback(@RequestBody FeedbackAddRequest feedbackAddRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(feedbackAddRequest == null, ErrorCode.PARAMS_ERROR);
        // 基本参数校验
        if (feedbackAddRequest.getAppId() == null || feedbackAddRequest.getAppId() <= 0
                || StringUtils.isBlank(feedbackAddRequest.getContent())) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数错误");
        }
        Feedback feedback = new Feedback();
        feedback.setAppId(feedbackAddRequest.getAppId());
        // 从当前登录用户获取 userId
        feedback.setUserId(userService.getLoginUser(request).getId());
        feedback.setContent(feedbackAddRequest.getContent());
        // 校验反馈数据（新增时）
        feedbackService.validFeedback(feedback, true);
        boolean result = feedbackService.save(feedback);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "反馈提交失败");
        return ResultUtils.success(feedback.getId());
    }

    /**
     * 根据 id 获取反馈封装类
     */
    @GetMapping("/get/vo")
    public BaseResponse<FeedbackVO> getFeedbackVOById(long id, HttpServletRequest request) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        Feedback feedback = feedbackService.getById(id);
        ThrowUtils.throwIf(feedback == null, ErrorCode.NOT_FOUND_ERROR);
        FeedbackVO feedbackVO = feedbackService.getFeedbackVO(feedback, request);
        return ResultUtils.success(feedbackVO);
    }

    /**
     * 分页获取反馈列表（仅管理员可用）
     */
    @PostMapping("/list/page")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<Feedback>> listFeedbackByPage(@RequestBody FeedbackQueryRequest queryRequest) {
        long current = queryRequest.getCurrent();
        long size = queryRequest.getPageSize();
        QueryWrapper<Feedback> queryWrapper = feedbackService.getQueryWrapper(queryRequest);
        Page<Feedback> feedbackPage = feedbackService.page(new Page<>(current, size), queryWrapper);
        return ResultUtils.success(feedbackPage);
    }

    /**
     * 删除反馈（仅管理员可用）
     *
     * @param deleteRequest 包含要删除反馈记录的 id
     * @param request HTTP 请求
     * @return 删除结果 true/false
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteFeedback(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = feedbackService.removeById(deleteRequest.getId());
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "删除反馈失败");
        return ResultUtils.success(result);
    }

}
